package com.hzit.dao;

import com.hzit.bean.Dept;
import com.hzit.bean.Emp;
import com.hzit.dao.base.BaseDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DeptDao extends BaseDao {

    /**
     * 获取所有员工列表
     *
     * @return
     */
    public List<Dept> findAll() {

        //1.获取连接
        Connection connection = this.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;

        List<Dept> deptList = new ArrayList<>();


        try {
            //2.编写 sql ,执行
            String sql = "SELECT * FROM dept";
            preparedStatement = connection.prepareStatement(sql);

            //1--> 0  2
            //2--> 2  2
            //3--> 4  2

            //起始参数  (page-1)*pageSize    pageSize
            // preparedStatement.setInt(1, (page - 1) * pageSize);
            //  preparedStatement.setInt(2, pageSize);


            //3.获得结果集
            rs = preparedStatement.executeQuery();

            //4.解析并返回结果

            Dept dept = null;
            while (rs.next()) {

                int deptno = rs.getInt("deptno");
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");

                dept = new Dept(deptno, dname, loc);

                deptList.add(dept);


            }

            return deptList;

        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("----->封装dept报错");
        } finally {
            this.closeAll(rs, preparedStatement, connection);
        }
        return null;
    }

}
